package com.mmbang.index;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Vector;
import java.util.regex.Pattern;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.bson.types.ObjectId;


//import com.mmbang.test.SearcherIk;
import com.mmbang.util.FileRes;
import com.mmbang.util.DbTools;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.webssky.jcseg.core.Config;
import com.webssky.jcseg.lucence.JcsegAnalyzer;

public class IndexTopic {
	//"d://test2"
	public static String indexFile = FileRes.getPropValue("indexFileForTopic");
	
	//索引
	public void index() {
		  File file = new File(indexFile);
		  FSDirectory directory = null;
		try {
			directory = FSDirectory.open(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println(e.getMessage());
		}
		  //用来创建索引
		  
		  Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
//		  Analyzer analyzer = new IKAnalyzer(true);
//		  Analyzer analyzer = new JcsegAnalyzer(Config.COMPLEX_MODE);
		  
		//  IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36,new StandardAnalyzer(Version.LUCENE_36));
		  IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36,analyzer);
		  //
		//  conf.setOpenMode(OpenMode.APPEND);
//		  conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
		  conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
		  IndexWriter writer;
		try {
//			writer = new IndexWriter(directory);
			writer = new IndexWriter(directory,conf);


			System.out.println("开始索引标题....");
			long startTime = new Date().getTime();
		
//		  this.fileToIndex(writer);
		  this.dbToIndex(writer);	
		  
		  writer.close();//一定要关闭writer，这样索引才能写到磁盘上
		  
			long entTime = new Date().getTime();		 
			System.out.println("索引结束....");
			System.out.println("索引总共花费了：" + ((entTime-startTime)/1000) + "秒");	  
		  
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public StringBuffer getPosts(Mongo mg,String topicId) {


    	StringBuffer strBuf = new StringBuffer();
		try {
	
	
	        // 获取名为“dbtest”的数据库对象
	        DB db = mg.getDB("bangs");


	        DBCollection posts = db.getCollection("posts");

	        BasicDBObject query=new BasicDBObject();  
	        query.put("topic_id", 100169);
	        
	        DBCursor cur = posts.find(query);

	        while (cur.hasNext()) {
	            DBObject object = cur.next();
//System.out.println(object);
	            // 取出对象中列表为字段名为'uname'和'upwd'的数据
	            //
	//            System.out.println("user_name:" + object.get("user_name") + "\tcontent:"+ object.get("content"));
	            String id = String.valueOf(object.get("_id"));
	            String user_name = String.valueOf(object.get("user_name"));
	            String content = String.valueOf(object.get("content"));
//	            System.out.println("id:" + id);
//	            System.out.println("user_name:" + user_name);
//	            System.out.println("content:" + content);
	            strBuf.append(content);
	            
	            
	        }
	       
	     
	        cur.close();
	        
	        
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}  
        
        return strBuf;
    
	}
	/*
	//从文件中尽心热门搜索词的索引
	public void fileToIndex(IndexWriter writer) {

		try {		
		
			FileRes fileRes = new FileRes();
			Vector vec_hotsearchword = fileRes.getHotSearchword();
			int orderNum =0;
			for(Object s:vec_hotsearchword) {
				
				orderNum++;
				String title = s.toString();
System.out.println("title:" + title);				
				
	        	int len = title.length(); 
	        	for(int i=0;i<len;i++){
	        		Document doc1 = new Document();
	        		
	        		 String value = title.substring(i,len);
		            	
		            	//doc1.add(new Field("pdate", StringUtil.null2String(gk.getPdate()), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));//日期不分词
	        		 
					doc1.add(new Field("title", title, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
					doc1.add(new Field("searchtitle", value, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
					//doc1.add(new Field("ordernum", String.valueOf(orderNum), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
					doc1.add(new IntField("ordernum", orderNum, Field.Store.YES));
						
					writer.addDocument(doc1);
	
						
						
	            	String searchTitle = pinYinTest001.getPinYin(value);
	            	
	            	Document doc2 = new Document();
	            	doc2.add(new Field("title",title,Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	            	doc2.add(new Field("searchtitle",searchTitle,Field.Store.YES, Field.Index.ANALYZED));
	            	//doc2.add(new Field("ordernum", String.valueOf(orderNum), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	            	doc2.add(new IntField("ordernum", orderNum, Field.Store.YES));
	            	writer.addDocument(doc2);
						
	            	String searchTitle3 = pinYinTest001.getPinYinFirst(value);
	            	
	            	Document doc3 = new Document();
	            	doc3.add(new Field("title",title,Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	            	doc3.add(new Field("searchtitle",searchTitle3,Field.Store.YES, Field.Index.ANALYZED));
	            	//doc3.add(new Field("ordernum", String.valueOf(orderNum), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	            	doc3.add(new IntField("ordernum", orderNum, Field.Store.YES));
	            	writer.addDocument(doc3);	    		
	        	}
			}
		
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
	}
	*/
	
	//数据库中进行索引
	public void dbToIndex(IndexWriter writer) {
    	Mongo mg;
		try {
			mg = new Mongo(FileRes.getPropValue("dbHost"),27017);


	        // 获取名为“dbtest”的数据库对象
	        DB db = mg.getDB("area");
	        // 查询该库中所有的集合 (相当于表)
	        for (String name : db.getCollectionNames()) {
	            System.out.println("Collection Name: " + name);
	        }
	        /*
	        DBCollection posts = db.getCollection("posts");
	        // 查询集合中所有的数据
	        DBCursor cur = posts.find();
	        System.out.println("Record Count:" + cur.count());
	        */
	        DBCollection topics = db.getCollection("topics");
	        // 查询集合中所有的数据
//	        DBCursor cur = topics.find().limit(1000);
//	        DBCursor cur = topics.find();
	          
	        
	        
	        DBObject sort = new BasicDBObject("_id",1);
	        BasicDBObject query=new BasicDBObject();  
//	        query.put("_id", 12893);
//	        DBCursor cur = topics.find(query).sort(sort).limit(20000);
	        DBCursor cur = topics.find(query).sort(sort);
	        
	        System.out.println("Record Count:" + cur.count());  
	        
	        
	        
	        
	        while (cur.hasNext()) {
	            DBObject object = cur.next();
//	            System.out.println(object);
	            // 取出对象中列表为字段名为'uname'和'upwd'的数据
	            //
	//            System.out.println("user_name:" + object.get("user_name") + "\tcontent:"+ object.get("content"));
//	            String id = object.get("_id").toString();
//	            if(object.get("user_name") == null) {
//	            	continue;
//	            }
//	            String user_name = object.get("user_name").toString();
//	            String title = object.get("title").toString();
//	            String content = object.get("content").toString();
	            
	            
//	            String title = String.valueOf(object.get("title"));
	            String id = String.valueOf(object.get("_id"));
				String user_id= String.valueOf(object.get("user"));
				String title= String.valueOf(object.get("title"));
				String content = String.valueOf(object.get("content"));
				//String image_list= String.valueOf(object.get("image_list"));
				String posts = String.valueOf(object.get("posts"));
				String cat = String.valueOf(object.get("cat"));
//System.out.println("user_id"+user_id);
				//String user_name = String.valueOf(DbTools.getUserObject(mg, new ObjectId(user_id)).get("name"));
				String user_name = "";
				DBObject user_object = DbTools.getUserObject(mg, new ObjectId(user_id));
				if (user_object!=null){
					user_name = String.valueOf(user_object.get("name"));
				}
//System.out.println("user_name"+user_name);				
				String create_time=String.valueOf(object.get("create_time"));
				String update_time=String.valueOf(object.get("update_time"));
				//String village_name = String.valueOf(DbTools.getVillageObject(mg, new ObjectId(village_id)).get("name"));		
				
	            
System.out.println("id:" + id);
System.out.println("user_name:" + user_name);

	            
	            
	            
	            try {
	            		Document doc1 = new Document();
	            		
//	            		String value = title.substring(i,len);
	  	            	
	  	            	//doc1.add(new Field("pdate", StringUtil.null2String(gk.getPdate()), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));//日期不分词
	  					doc1.add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED));
	  					
	  					doc1.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
	  					
	  					doc1.add(new Field("posts", posts, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	  					doc1.add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	  					doc1.add(new Field("user_id", user_id, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	  					doc1.add(new Field("cat", cat, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	  					doc1.add(new Field("user_name", user_name, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	  					doc1.add(new Field("create_time", create_time, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	  					doc1.add(new Field("update_time", update_time, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
	  					

	  					writer.addDocument(doc1);
	  					
	  					
	  					/*
		            	String searchTitle = pinYinTest001.getPinYin(value);
		            	
		            	Document doc2 = new Document();
		            	doc2.add(new Field("title",title,Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
		            	doc2.add(new Field("searchtitle",searchTitle,Field.Store.YES, Field.Index.ANALYZED));
		            	//doc2.add(new Field("ordernum", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
		            	doc2.add(new IntField("ordernum", 10000, Field.Store.YES));
		            	writer.addDocument(doc2);
	  					
		            	
		            	//String searchTitle3 = pinYinTest001.getPinYinFirst(value);
		            	String searchTitle3 = pinYinTest001.getPinYinFirst(value);
		            	
		            	Document doc3 = new Document();
		            	doc3.add(new Field("title",title,Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
		            	doc3.add(new Field("searchtitle",searchTitle3,Field.Store.YES, Field.Index.ANALYZED));
		            	//doc3.add(new Field("ordernum", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
		            	doc3.add(new IntField("ordernum", 10000, Field.Store.YES));
		            	writer.addDocument(doc3);
		            	*/
		            	/**/
	            	/**/
	            	

					
					
				} catch (CorruptIndexException e) {
					// TODO Auto-generated catch block
//					e.printStackTrace();
					System.out.println("1111111"+ e.getMessage());
				} catch (IOException e) {
					// TODO Auto-generated catch block
//					e.printStackTrace();
					System.out.println("222222222"+ e.getMessage());
				}
	        }
	        cur.close();
	        writer.commit();
	        
//	        writer.optimize();
		
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("3333333333:" + e.getMessage());
		}
        
	}
	
	public void search() throws IOException {
		  File file = new File(indexFile);
		  FSDirectory directory = FSDirectory.open(file);
		IndexReader reader = IndexReader.open(directory);
		  //简单索引
		  IndexSearcher searcher = new IndexSearcher(reader);
		  Term t = new Term("title", "欢迎大家来到妈妈帮");
		     TermQuery quenry = new TermQuery(t);
		     //获得得分靠前的两个匹配记录
		     ScoreDoc[] docs = searcher.search(quenry, 100).scoreDocs;
		     for(int i = 0; i < docs.length; i++) {
		      Date start = new Date();
		      String title = searcher.doc(docs[i].doc).get("title");
		      String id = searcher.doc(docs[i].doc).get("id");
		      Date end = new Date();
		      System.out.println("id:" + id);
		      System.out.println("title:" + title);
		     }
		  
		  }
	/*
	public void search1() throws IOException {	
		  File file = new File(indexFile);
		  FSDirectory directory = FSDirectory.open(file);
		IndexReader reader = IndexReader.open(directory);

		  //简单索引
		  IndexSearcher searcher = new IndexSearcher(reader);
		  Term t = new Term("title", "90后妈妈");
		  TermQuery quenry = new TermQuery(t);
		  searcher.s
		  
		
	    //构建一个term对象
	    Term term=new Term("name","女");
	    //构建一个query对象
	    Query q =new TermQuery (term);
	    //检索
	    Hits hits=reader.
	    //显示结果
	    for(int i=0;i<hits.lengtth();i++){
	        system.out.println(hits.doc(i));
	    }
	
	}
	*/
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		IndexTopic test = new IndexTopic();
		//索引
		test.index();
		
	}

}
